package com.typ.member.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.typ.member.common.enums.OpTypeEnum;
import com.typ.member.common.enums.StatusEnum;
import com.typ.member.common.excepiton.BadArgumentException;
import com.typ.member.common.excepiton.BusinessException;
import com.typ.member.common.model.PageRequest;
import com.typ.member.common.utils.CodeUtil;
import com.typ.member.dao.mapper.MemberTradeDtoMapper;
import com.typ.member.dao.mapper.MemberTradeDtoMapper;
import com.typ.member.dao.model.MemberTradeDto;
import com.typ.member.dao.model.MemberTradeQueryDto;
import com.typ.member.dao.model.MemberTradeDto;
import com.typ.member.service.MemberTradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 会员卡信息接口
 *
 * @author heran
 * @version 1.0, 2019-05-10 13:32
 */
@Service
@Slf4j
public class MemberTradeServiceImpl implements MemberTradeService {

    @Resource
    private MemberTradeDtoMapper memberTradeDtoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(MemberTradeDto memberTradeDto) {
        //参数校验
        paramCheck(memberTradeDto);

        //补全必要的信息
        fillMemberTradeDto(memberTradeDto);

        //保存会员卡信息
        memberTradeDtoMapper.insert(memberTradeDto);

    }

    @Override
    public void update(MemberTradeDto memberTradeDto) {
        if (memberTradeDto == null || memberTradeDto.getId() == null) {
            throw new BadArgumentException("交易ID不能为空");
        }
        memberTradeDtoMapper.updateByPrimaryKeySelective(memberTradeDto);
    }

    @Override
    public void delete(Long id) {
        memberTradeDtoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<MemberTradeDto> search(MemberTradeQueryDto memberTradeQueryDto, PageRequest pageRequest) {
        Page<MemberTradeDto> page = PageHelper.startPage(pageRequest.getPage(), pageRequest.getPageSize());
        List<MemberTradeDto> memberTradeDtos = memberTradeDtoMapper.selectByQuery(memberTradeQueryDto);
        PageInfo<MemberTradeDto> pageInfo = new PageInfo<>(page);
        pageInfo.setList(memberTradeDtos);
        return pageInfo;
    }

    /**
     * 补全必要的信息
     *
     * @param memberTradeDto
     */
    private void fillMemberTradeDto(MemberTradeDto memberTradeDto) {
        memberTradeDto.setStatus(StatusEnum.NORMAL.getCode());
        memberTradeDto.setCreated(new Date());
        memberTradeDto.setModified(new Date());
    }

    /**
     * 参数验证
     *
     * @param memberTradeDto
     */
    private void paramCheck(MemberTradeDto memberTradeDto) {
        if (memberTradeDto == null) {
            throw new BadArgumentException("会员交易记录不能为空");
        }
        if (memberTradeDto.getCardId() == null) {
            throw new BadArgumentException("会员交易记录-会员卡ID不能为空");
        }
        if (StringUtils.isEmpty(memberTradeDto.getName())) {
            throw new BadArgumentException("会员交易记录-名称不能为空");
        }
        if (StringUtils.isEmpty(memberTradeDto.getPhone())) {
            throw new BadArgumentException("会员交易记录-电话号码不能为空");
        }
    }

}
